package net.cherrylab.manager
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.setTimeout;
	
	import net.cherrylab.Event.ParticleEvent;
	import net.cherrylab.component.Ball;
	import net.cherrylab.component.Particle;
	import net.cherrylab.tools.ParticleEffect;

	/**
	 *粒子效果	管理器，生成各种粒子效果
	 * 
	 * @Auther Rolland Chen
	 * @Date 2010.10.19
	 * 
	 * */
	public class ParticleManager
	{
		/**
		 * 全屏
		 * */
		public static const FULL_SCREEN:String = "full_screen";
		
		/**
		 * 点对面
		 * Point to Surface
		 * 
		 * */
		public static const PTS:String = "point_opposite";
		
		/**
		 * 点对点
		 * Point to Point 
		 * 
		 * */
		public static const PTP:String = "point_to_point";
		
		/**
		 * 连线
		 * Line 
		 * 
		 * */
		public static const LINE:String = "line";
		
		/**
		 * 通用系数
		 * */
		private static var veasing:int=1;
		
		/**
		 * 通用方向
		 * */
		private static var direction:int=1;
		
		public function ParticleManager()
		{
		}
		
		/**
		 * 设置效果类型
		 * */
		public static function creatEffect(type:String,pe:ParticleEffect):void
		{
			
		}
		
		
		
		/**
		 * 粒子在一个圆形的范围内做随机分布
		 * */
		public static function CircleRange(pe:ParticleEffect):void
		{
			//var sp:Sprite=new Sprite();
			var m:int=pe.numParticles-pe.particles.length;
			var i:uint ;
			for (i= 0; i < m; i++)
			{ 
				var dot:Ball = new Ball(1); 
				
				pe.addChild(dot); 
				pe.particles.push(dot);
			} 
			for ( i= 0; i < pe.particles.length; i++)
			{ 
				
				////在圆形范围内随机生成粒子坐标点
				var radius:Number = Math.sqrt(Math.random()) * pe.radius; 
				var angle:Number = Math.random() * 
					(Math.PI * 2); 
				pe.particles[i].x = pe.range.width/ 2 + 
					Math.cos(angle) * radius; 
				pe.particles[i].y =pe.range.height/ 2 + 
					Math.sin(angle) * radius; 
			} 
			var e:ParticleEvent=new ParticleEvent(ParticleEvent.PARTICLE_ENTERFRAME_EVENT);
			e.fun=moveToPoint;
			pe.dispatchEvent(e);
		}
		
		
		/**
		 * 向目标范围随机发射(生成)
		 * */
		public static function PointToRange(pe:ParticleEffect):void
		{
			var m:int=pe.numParticles-pe.particles.length;
			var i:uint ;
			if(!pe.targetArray)
			{
				pe.targetArray=new Array();
			}
			for (i= 0; i < m; i++)
			{ 
				var dot:Ball = new Ball(1); 
				
				pe.addChild(dot); 
				pe.particles.push(dot);
			} 
			for ( i= 0; i < pe.particles.length; i++)
			{ 
				//初始化粒子坐标
				pe.particles[i].x = pe.outsetPoint.x;
				pe.particles[i].y =pe.outsetPoint.y; 
				//生成随机目标点
				var radius:Number = Math.sqrt(Math.random()) * pe.radius; 
				var angle:Number = Math.random() * 
					(Math.PI * 2); 
				var p:Point=new Point();
				p.x = pe.range.width/ 2 + 
					Math.cos(angle) * radius; 
				p.y =pe.range.height/ 2 + 
					Math.sin(angle) * radius; 
				pe.targetArray[i]=p;
			} 
			
			var e:ParticleEvent=new ParticleEvent(ParticleEvent.PARTICLE_ENTERFRAME_EVENT);
			e.fun=MoveToRange;
			pe.dispatchEvent(e);
		}
		
		/**
		 * 全屏随机生成粒子
		 * */
		public static function PointFullScreen(pe:ParticleEffect):void
		{
			//var sp:Sprite=new Sprite();
			var m:int=pe.numParticles-pe.particles.length;
			var i:uint ;
			for (i= 0; i < m; i++)
			{ 
				var dot:Ball = new Ball(1); 
				
				pe.addChild(dot); 
				pe.particles.push(dot);
			} 
			for ( i= 0; i < pe.particles.length; i++)
			{ 
				
				
				pe.particles[i].x =Math.floor(Math.random()*pe.range.width);
				pe.particles[i].y =Math.floor(Math.random()*pe.range.height);
			} 
			var e:ParticleEvent=new ParticleEvent(ParticleEvent.PARTICLE_ENTERFRAME_EVENT);
			e.fun=downToLeft;
			pe.dispatchEvent(e);
			setTimeout(pe.removeEnterFrame,4000);
		}
		
		/**
		 * 生成几个对称的粒子
		 * */
		public static function  creatPartic(pe:ParticleEffect):void
		{
			veasing=1;
			var m:int=pe.numParticles-pe.particles.length;
			var i:uint ;
			for (i= 0; i < m; i++)
			{ 
				var dot:Ball = new Ball(1); 
				
				pe.addChild(dot); 
				pe.particles.push(dot);
			} 
			for ( i= 0; i < pe.particles.length; i++)
			{ 
				
				var angle:Number=  Math.PI * 2/pe.numParticles;
				pe.particles[i].x = pe.outsetPoint.x + Math.cos(angle*i) * pe.radius; 
				pe.particles[i].y = pe.outsetPoint.y + Math.sin(angle*i) * pe.radius; 
				//pe.targetArray[i]=p;
			} 
			var e:ParticleEvent=new ParticleEvent(ParticleEvent.PARTICLE_ENTERFRAME_EVENT);
			e.fun=roundToPoint;
			pe.dispatchEvent(e);
			//setTimeout(pe.removeEnterFrame,4000);
		}
		
		///////////////////////////////////////onEnterFrame////////////////////////////////////////////////////////////////////////////////
		
		/**
		 * 绕圆心旋转
		 * */
		public static function roundToPoint(pe:ParticleEffect):void
		{
			var m:int=pe.particles.length;
			var i:uint ;
			
			for (i = 0; i <m; i++)
			{ 
				var sprite:Sprite = pe.particles[i]; 
				var dx:Number = sprite.x - pe.outsetPoint.x; 
				var dy:Number = sprite.y -pe.outsetPoint.y; 
				var angle:Number = Math.atan2(dy, dx); 
				var dist:Number = Math.sqrt(dx * dx + dy * dy)+veasing*0.1*direction; 
				angle += 0.01*veasing; 
				sprite.x =  pe.outsetPoint.x + Math.cos(angle) * dist; 
				sprite.y = pe.outsetPoint.y + Math.sin(angle) * dist; 
			}
			if(veasing>=20)
			{
				direction=-1;
			}
			if(veasing<=10)
			{
				direction=1;
			}
			veasing+=direction;
		}
		
		
		/**
		 * 斜四十五度向下运动
		 * */
		public static function downToLeft(pe:ParticleEffect):void
		{
			var i:int;
			var m:int=pe.particles.length;
			if(veasing>10)
			{
				veasing=1;
			}
			
			for(i=0;i<m;i++)
			{
				var v:Number=i*1+1; 
				if(v>10)
				{
					v=10;
				}
				
				if((pe.particles[i].x-v)<0)
				{
					pe.particles[i].x =Math.floor(Math.random()*pe.range.width);
					pe.particles[i].y =Math.floor(Math.random()*pe.range.height*0.5);
				}
				if((pe.particles[i].y+v)>(pe.range.height))
				{
					pe.particles[i].x =Math.floor(Math.random()*pe.range.width*0.5+pe.range.width*0.5);
					pe.particles[i].y =Math.floor(Math.random()*pe.range.height);
				}
				pe.particles[i].x-= v; 
				pe.particles[i].y+= v;	
								
			}
			veasing+=0.5;
		}
		
		
		/**
		 * 向一个目标点运动
		 * 
		 * */
		public static function moveToPoint(pe:ParticleEffect):void
		{
			var i:int;
			var m:int=pe.particles.length;
			for(i=0;i<m;i++)
			{
				var dx:Number=Math.abs(pe.targetPoint.x - pe.particles[i].x);
				var dy:Number=Math.abs(pe.targetPoint.y - pe.particles[i].y);
				
				if((dx<1)&&(dy<1))
				{
					pe.particles[i].x =pe.targetPoint.x; 
					pe.particles[i].y =pe.targetPoint.y; 
					if(i==(m-1))
					{
						pe.removeEnterFrame();
					}
					
					
				}else{
					var vx:Number=(pe.targetPoint.x - pe.particles[i].x) * pe.easing; 
					var vy:Number=(pe.targetPoint.y - pe.particles[i].y) * pe.easing; 
					pe.particles[i].x+= vx; 
					pe.particles[i].y+= vy;
				}
				
			}
		}
		
		
		
		/**
		 * 向目标范围随机发射(运行)
		 * */
		public static function MoveToRange(pe:ParticleEffect):void
		{
			var i:int;
			var m:int=pe.particles.length;
			for(i=0;i<m;i++)
			{
				var dx:Number=Math.abs(pe.targetArray[i].x - pe.particles[i].x);
				var dy:Number=Math.abs(pe.targetArray[i].y - pe.particles[i].y);
				
				if((dx<1)&&(dy<1))
				{
					pe.particles[i].x =pe.targetArray[i].x; 
					pe.particles[i].y =pe.targetArray[i].y; 
					
					if(i==(m-1))
					{
						pe.removeEnterFrame();
					}
					
					
				}else{
					/*var v:Number=Math.random()*0.5;
					pe.easing=v;*/
					var vx:Number=(pe.targetArray[i].x - pe.particles[i].x) * pe.easing; 
					var vy:Number=(pe.targetArray[i].y - pe.particles[i].y) * pe.easing; 
					pe.particles[i].x+= vx; 
					pe.particles[i].y+= vy;
				}
				
			}
		}
		
		
		
		//万有引力吸引
		private function gravitate(partA:Ball, partB:Ball):void { 
			var dx:Number = partB.x - partA.x; 
			var dy:Number = partB.y - partA.y; 
			var distSQ:Number = dx * dx + dy * dy; 
			var dist:Number = Math.sqrt(distSQ); 
			var force:Number = partA.mass * partB.mass / distSQ; 
			var ax:Number = force * dx / dist; 
			var ay:Number = force * dy / dist; 
			partA.vx += ax / partA.mass; 
			partA.vy += ay / partA.mass; 
			partB.vx -= ax / partB.mass; 
			partB.vy -= ay / partB.mass; 
		}
		
		
		
		
		
		
	}
}